/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.prolificinteractive.materialcalendarview.sample;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.text.Font;
import ohos.agp.window.dialog.ToastDialog;
import ohos.bundle.AbilityInfo;

import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.CalendarMode;
import com.prolificinteractive.materialcalendarview.DayViewDecorator;
import com.prolificinteractive.materialcalendarview.DayViewFacade;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
import com.prolificinteractive.materialcalendarview.OnDateLongClickListener;
import com.prolificinteractive.materialcalendarview.OnDateSelectedListener;
import com.prolificinteractive.materialcalendarview.OnMonthChangedListener;
import com.prolificinteractive.materialcalendarview.format.ArrayWeekDayFormatter;
import com.prolificinteractive.materialcalendarview.sample.tools.FontCache;

import org.threeten.bp.DayOfWeek;
import org.threeten.bp.format.DateTimeFormatter;

/**
 * @since 2021-07-12
 */
public class RotationAbility extends BaseAbility implements OnDateLongClickListener, OnMonthChangedListener, OnDateSelectedListener {

    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("EEE, d MMM yyyy");
    private Text textView;
    MaterialCalendarView widget, widgetcopy;
    Integer i = 0;
    CalendarDay date;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        widget = (MaterialCalendarView) findComponentById(ResourceTable.Id_calendarView);
        widgetcopy = (MaterialCalendarView) findComponentById(ResourceTable.Id_calendarViewcopy);
        widget.setOnDateChangedListener(this);
        widget.setOnDateLongClickListener(this);
        widget.setOnMonthChangedListener(this);
        widgetcopy.setOnDateChangedListener(this);
        widgetcopy.setOnDateLongClickListener(this);
        widgetcopy.setOnMonthChangedListener(this);
        toolbarTitle.setText("Rotation Example");
        widget.setSelectionColor(COLOR_ACCENT);
        widgetcopy.setSelectionColor(COLOR_ACCENT);

        Font mFont = FontCache.get(this, "shrikhand_regular.ttf");
//        设置星期字体 非中文
        widgetcopy.setWeekDayTextAppearance(mFont);
//        设置年月字体 非中文
        widgetcopy.setHeaderTextAppearance(mFont);
//        设置字体捆绑
        widgetcopy.setDateTextAppearance(mFont);
        widgetcopy.addDecorators(new EnableOneToTenDecorator());

        CharSequence[] weekDayLabels = {"M", "Tu", "W", "Th", "F", "Sa", "Su"};
        widgetcopy.setWeekDayFormatter(new ArrayWeekDayFormatter(weekDayLabels));
        widgetcopy.setTileSize(108);
        widgetcopy.setTitleAnimationOrientation(MaterialCalendarView.VERTICAL);
        widgetcopy.state().edit()
            .setFirstDayOfWeek(DayOfWeek.SUNDAY)
            .setCalendarDisplayMode(CalendarMode.WEEKS)
            .setShowWeekDays(false)
            .commit();
        if (getAbilityInfo().getOrientation() == AbilityInfo.DisplayOrientation.PORTRAIT) {
            widget.setVisibility(Component.VISIBLE);
            widgetcopy.setVisibility(Component.HIDE);
        } else if (getAbilityInfo().getOrientation() == AbilityInfo.DisplayOrientation.UNSPECIFIED) {
            widget.setVisibility(Component.VISIBLE);
            widgetcopy.setVisibility(Component.HIDE);
        } else if (getAbilityInfo().getOrientation() == AbilityInfo.DisplayOrientation.LANDSCAPE) {
            widget.setVisibility(Component.HIDE);
            widgetcopy.setVisibility(Component.VISIBLE);
        }
    }

    @Override
    int contentLayoutRes() {
        return ResourceTable.Layout_ability_rotation;
    }

    @Override
    protected void onOrientationChanged(AbilityInfo.DisplayOrientation displayOrientation) {
        super.onOrientationChanged(displayOrientation);
        if (displayOrientation.name().equals("PORTRAIT")) {
            widgetcopy.setVisibility(Component.HIDE);
            widget.setVisibility(Component.VISIBLE);
        } else if (displayOrientation.name().equals("LANDSCAPE")) {
            widgetcopy.setVisibility(Component.VISIBLE);
            widget.setVisibility(Component.HIDE);
        }
        i++;
            toolbarTitle.setText(FORMATTER.format(date.getDate()));
    }

    @Override
    public void onDateLongClick(MaterialCalendarView widget, CalendarDay date) {
        final String text = String.format("%s is available", FORMATTER.format(date.getDate()));
        new ToastDialog(this).setText(text).show();
    }

    @Override
    public void onMonthChanged(MaterialCalendarView widget, CalendarDay date) {
        this.date = date;
        if(i>1) {
            toolbarTitle.setText(FORMATTER.format(date.getDate()));
        }
    }

    @Override
    public void onDateSelected(MaterialCalendarView widget, CalendarDay date, boolean selected) {
        this.widget.setSelectedDate(date);
        widgetcopy.setSelectedDate(date);
    }

    private static class EnableOneToTenDecorator implements DayViewDecorator {

        @Override
        public boolean shouldDecorate(CalendarDay day) {
            return day.getDay() <= 31;
        }

        @Override
        public void decorate(DayViewFacade view) {
            view.setDaysDisabled(false);
        }
    }
}
